package com.quectel.core.module.scheduling.service.impl;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.quectel.constant.global.SystemConstants;
import com.quectel.core.constants.RedisCacheConstants;
import com.quectel.core.module.scheduling.dao.SchedulingAttendanceDailyTimeDao;
import com.quectel.core.module.scheduling.dto.SchedulingAttendanceDailyTimeDto;
import com.quectel.core.module.scheduling.entity.SchedulingAttendanceDailyTimeEntity;
import com.quectel.core.module.scheduling.service.SchedulingAttendanceDailyTimeService;
import com.quectel.util.common.CopyUtils;
import com.quectel.util.kit.CacheKit;
import com.quectel.util.kit.Snowflake;
import com.quectel.util.mybatis.MyBatisPlusUtils;
import org.apache.dubbo.config.annotation.DubboService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Map;
import java.util.function.Consumer;
import java.util.function.Function;
/**
 *
 * @author ryan
 * @email ryan.ran@quectel.com
 * @date 2023/07/13 18:58
 */
@DubboService
public class SchedulingAttendanceDailyTimeServiceImpl extends ServiceImpl<SchedulingAttendanceDailyTimeDao, SchedulingAttendanceDailyTimeEntity> implements SchedulingAttendanceDailyTimeService{

    private static final Logger LOGGER = LoggerFactory.getLogger(SchedulingAttendanceDailyTimeServiceImpl.class);

    /**
     * 入参解释:实体id
     */
    private static final Function<Long, String> SCHEDULING_ATTENDANCE_DAILY_TIME_CACHE_BY_ID_FUNC = id -> String.format(RedisCacheConstants.PROJECT_NAME + "SCHEDULING_ATTENDANCE_DAILY_TIME_CACHE_BY_ID:%d", id);

    /**
     * 清除缓存方法
     */
    private static final Consumer<SchedulingAttendanceDailyTimeDto> CLEAR_CACHE_FUNC = dto -> {
        CacheKit.invalidRedisCache(SCHEDULING_ATTENDANCE_DAILY_TIME_CACHE_BY_ID_FUNC.apply(dto.getId()));
    };

    @Autowired
    private SchedulingAttendanceDailyTimeDao schedulingAttendanceDailyTimeDao;


    @Override
    public void deleteByDailyId(Long dailyId) {
        schedulingAttendanceDailyTimeDao.delete(new LambdaQueryWrapper<SchedulingAttendanceDailyTimeEntity>()
                .eq(SchedulingAttendanceDailyTimeEntity::getAttendanceDailyId, dailyId));
    }

    @Override
    public List<SchedulingAttendanceDailyTimeDto> selectByDailyId(Long id) {
        List<SchedulingAttendanceDailyTimeEntity> schedulingAttendanceDailyTimeEntities = schedulingAttendanceDailyTimeDao.selectList(new LambdaQueryWrapper<SchedulingAttendanceDailyTimeEntity>()
                .eq(SchedulingAttendanceDailyTimeEntity::getAttendanceDailyId, id));
        List<SchedulingAttendanceDailyTimeDto> timeDtos = CopyUtils.copyList(schedulingAttendanceDailyTimeEntities, SchedulingAttendanceDailyTimeDto.class);
        timeDtos.forEach(this::paddingField);
        return timeDtos;
    }

    @Override
    public void saveBatch(List<SchedulingAttendanceDailyTimeDto> timeDtoList) {
        List<SchedulingAttendanceDailyTimeEntity> schedulingAttendanceDailyTimeEntities = CopyUtils.copyList(timeDtoList, SchedulingAttendanceDailyTimeEntity.class);
        for (SchedulingAttendanceDailyTimeEntity schedulingAttendanceDailyTimeEntity : schedulingAttendanceDailyTimeEntities) {
            schedulingAttendanceDailyTimeEntity.setId(Snowflake.nextId());
        }
        super.saveBatch(schedulingAttendanceDailyTimeEntities);
    }

    @Override
    public SchedulingAttendanceDailyTimeDto selectCacheById(Long id) {
        return CacheKit.cacheToRedis(
                () -> selectById(id),
                SCHEDULING_ATTENDANCE_DAILY_TIME_CACHE_BY_ID_FUNC.apply(id),
                SystemConstants.NOT_NULL_CACHE_EXPIRE_SECONDS
        );
    }

    @Override
    public SchedulingAttendanceDailyTimeDto selectById(Long id){
            SchedulingAttendanceDailyTimeEntity entity = schedulingAttendanceDailyTimeDao.selectById(id);
        return paddingField(CopyUtils.copyObj(entity,SchedulingAttendanceDailyTimeDto.class));
    }

    @Override
    public List<SchedulingAttendanceDailyTimeDto> queryList(Map<String,Object> params){
        List<SchedulingAttendanceDailyTimeEntity> list = schedulingAttendanceDailyTimeDao.queryList(params);
        List<SchedulingAttendanceDailyTimeDto> result =  CopyUtils.copyList(list,SchedulingAttendanceDailyTimeDto.class);
        result.forEach(this::paddingField);
        return result;
    }

    @Override
    public List<SchedulingAttendanceDailyTimeDto> queryListWithoutPadding(Map<String,Object> params){
        List<SchedulingAttendanceDailyTimeEntity> list = schedulingAttendanceDailyTimeDao.queryList(params);
        return CopyUtils.copyList(list,SchedulingAttendanceDailyTimeDto.class);
    }

    /**
     * 此方法慎用 禁止填充List一类 比如一个人有多个地址这里不允许填充
     * 填充的原则是: 1:被填充对象不经常变换 2:根据id可以走缓存 3数据足够简单 比如设备类型 所属园区等
     *
     * @param dto
     * @return
     */
    private SchedulingAttendanceDailyTimeDto paddingField(SchedulingAttendanceDailyTimeDto dto){
        if (dto != null){

        }
        return dto;
    }

    @Override
    public int queryTotal(Map<String, Object> params){
        return schedulingAttendanceDailyTimeDao.queryTotal(params);
    }

    @Override
    public Long save(SchedulingAttendanceDailyTimeDto dto){
        SchedulingAttendanceDailyTimeEntity entity = CopyUtils.copyObj(dto,SchedulingAttendanceDailyTimeEntity.class);
        entity.setId(Snowflake.nextId());
        schedulingAttendanceDailyTimeDao.insert(entity);
        return entity.getId();
    }

    @Override
    public void updateById(SchedulingAttendanceDailyTimeDto dto){
        SchedulingAttendanceDailyTimeEntity entity = CopyUtils.copyObj(dto,SchedulingAttendanceDailyTimeEntity.class);

        schedulingAttendanceDailyTimeDao.updateById(entity);

        CLEAR_CACHE_FUNC.accept(dto);
    }

    @Override
    public void updateAllColumnById(SchedulingAttendanceDailyTimeDto dto) {
       SchedulingAttendanceDailyTimeEntity entity = CopyUtils.copyObj(dto,SchedulingAttendanceDailyTimeEntity.class);

       MyBatisPlusUtils.updateAllColumnById(entity, schedulingAttendanceDailyTimeDao);

       CLEAR_CACHE_FUNC.accept(dto);
    }

    @Override
    public void deleteById(Long id){

        CLEAR_CACHE_FUNC.accept(selectById(id));

        schedulingAttendanceDailyTimeDao.deleteById(id);

    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void deleteBatch(Long[] ids){
        for (Long id : ids) {
            deleteById(id);
        }
    }
}
